package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.Customer;
import com.caocaog.market.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 客户
 *
 * @author jeecg-boot
 * @date 2022-03-09
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "customer", tags="客户")
@RequestMapping("/customer")
public class CustomerController extends JeecgController<Customer, CustomerService> {

	private final CustomerService customerService;

	/**
	 * 分页列表查询
	 */
	@AutoLog(value = "客户-分页列表查询")
	@ApiOperation(value="客户-分页列表查询", notes="客户-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Customer>> queryPageList(Customer customer, QueryRequest queryRequest) {
		Page<Customer> page = PageGenerator.createPageSort(queryRequest);
		IPage<Customer> pageList = this.customerService.queryPage(page, customer);
		return Result.OK(pageList);
	}


	/**
	 * 添加
	 */
	@AutoLog(value = "客户-添加")
	@ApiOperation(value="客户-添加", notes="客户-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Customer customer) {
		customer.setCreateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());
		customerService.save(customer);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 */
	@AutoLog(value = "客户-编辑")
	@ApiOperation(value="客户-编辑", notes="客户-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Customer customer) {
		customerService.updateById(customer);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 */
	@AutoLog(value = "客户-通过id删除")
	@ApiOperation(value="客户-通过id删除", notes="客户-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		customerService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 */
	@AutoLog(value = "客户-批量删除")
	@ApiOperation(value="客户-批量删除", notes="客户-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.customerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 */
	@AutoLog(value = "客户-通过id查询")
	@ApiOperation(value="客户-通过id查询", notes="客户-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Customer customer = customerService.getById(id);
		if(customer==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(customer);
	}

}
